﻿using System;
using System.Xml;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using System.Reflection;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace AutoParkingManagement
{
    public abstract class AppConfig
    {
        #region Private members
        /// <summary>
        /// System configuration object
        /// </summary>
        private static Configuration objConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        #endregion

        #region Public members

        /// <summary>
        /// URL connect to the left front Camera
        /// </summary>
        public static string IPCameraLeftFront
        {
            get
            {
                return objConfig.AppSettings.Settings["IPCameraLeftFront"].Value.ToString();
            }
        }

        /// <summary>
        /// URL connect to the left rear Camera
        /// </summary>
        public static string IPCameraLeftRear
        {
            get
            {
                return objConfig.AppSettings.Settings["IPCameraLeftRear"].Value.ToString();
            }
        }

        /// <summary>
        /// URL connect to the right front Camera
        /// </summary>
        public static string IPCameraRightFront
        {
            get
            {
                return objConfig.AppSettings.Settings["IPCameraRightFront"].Value.ToString();
            }
        }

        /// <summary>
        /// URL connect to the right rear Camera
        /// </summary>
        public static string IPCameraRightRear
        {
            get
            {
                return objConfig.AppSettings.Settings["IPCameraRightRear"].Value.ToString();
            }
        }

        public static string HasIPCamera
        {
            get
            {
                return objConfig.AppSettings.Settings["HasIPCamera"].Value.ToString();
            }
        }

        public static string HasCardReader
        {
            get
            {
                return objConfig.AppSettings.Settings["HasCardReader"].Value.ToString();
            }
        }

        public static string HasImageToText
        {
            get
            {
                return objConfig.AppSettings.Settings["HasImageToText"].Value.ToString();
            }
        }

        public static string FrontVideoFile
        {
            get
            {
                return objConfig.AppSettings.Settings["FrontVideoFile"].Value.ToString();
            }
        }

        public static string RearVideoFile
        {
            get
            {
                return objConfig.AppSettings.Settings["RearVideoFile"].Value.ToString();
            }
        }

        public static int RFIDLEFT
        {
            get
            {
                try
                {
                    int intRet = 1;
                    int.TryParse(objConfig.AppSettings.Settings["RFIDLEFT"].Value.ToString(), out intRet);
                    return intRet;
                }
                catch
                {
                    return 1;
                }                
            }
        }

        public static int RFIDRIGHT
        {
            get
            {
                try
                {
                    int intRet = 2;
                    int.TryParse(objConfig.AppSettings.Settings["RFIDRIGHT"].Value.ToString(), out intRet);
                    return intRet;
                }
                catch
                {
                    return 2;
                }
            }
        }

        public static int CardReaderTimer
        {
            get
            {
                try
                {
                    int intRet = 1;
                    int.TryParse(objConfig.AppSettings.Settings["CardReaderTimer"].Value.ToString(), out intRet);
                    return intRet*1000;
                }
                catch
                {
                    return 1000;
                }
            }
        }

        public static string ParkingFloor
        {
            get
            {
                return objConfig.AppSettings.Settings["ParkingFloor"].Value;
            }
        }

        public static string IpServer
        {
            get
            {
                return objConfig.AppSettings.Settings["IpServer"].Value;
            }
        }

        public static int RefreshTime
        {
            get
            {
                try
                {
                    string tmp = objConfig.AppSettings.Settings["RefreshTime"].Value;
                    int retvalue = 0;
                    if (int.TryParse(tmp, out retvalue))
                    {
                        return retvalue;
                    }
                    return 0;
                }
                catch
                {
                    return 0;
                }
            }
        }

        /// <summary>
        /// Connection String to connect to database
        /// </summary>
        public static string ConnectionString
        {
            get
            {
                return objConfig.ConnectionStrings.ConnectionStrings["ConnectionString"].ConnectionString;

            }
        }

        /// <summary>
        /// Connection information
        /// </summary>
        public static SqlConnectionStringBuilder ConnectionInfo
        {
            get
            {
                SqlConnectionStringBuilder builder =
                    new SqlConnectionStringBuilder(ConnectionString);

                return builder;
            }
        }


        /// <summary>
        /// Save configuration file
        /// </summary>
        /// <param name="dictAppSettings">Application Settings</param>
        /// <param name="dictConnectionStrings">Connection Strings</param>
        public static void SaveConfig(Dictionary<String, String> dictAppSettings, Dictionary<String, String> dictConnectionStrings)
        {
            if (dictAppSettings != null)
            {
                foreach (KeyValuePair<String, String> setting in dictAppSettings)
                {
                    objConfig.AppSettings.Settings[setting.Key].Value = setting.Value;
                }
            }

            if (dictConnectionStrings != null)
            {
                foreach (KeyValuePair<String, String> connectionString in dictConnectionStrings)
                {
                    objConfig.ConnectionStrings.ConnectionStrings[connectionString.Key].ConnectionString = connectionString.Value;
                }
            }
            objConfig.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
            ConfigurationManager.RefreshSection("connectionStrings");
        }

        /// <summary>
        /// Build connection string base on datasource, user
        /// </summary>
        /// <param name="dataSource">Data source</param>
        /// <param name="userName">Username</param>
        /// <param name="userPassword">Password</param>
        /// <returns>Connection string</returns>
        public static string BuildConnectionString(string dataSource, string initialCatalog, string userName, string userPassword)
        {
            // Retrieve the partial connection string named databaseConnection
            // from the application's app.config or web.config file.
            ConnectionStringSettings settings = ConfigurationManager.ConnectionStrings["ConnectionString"];

            if (null != settings)
            {
                // Retrieve the partial connection string.
                string connectString = settings.ConnectionString;

                // Create a new SqlConnectionStringBuilder based on the
                // partial connection string retrieved from the config file.
                SqlConnectionStringBuilder builder =
                    new SqlConnectionStringBuilder(connectString);

                // Supply the additional values.
                builder.DataSource = dataSource;
                builder.InitialCatalog = initialCatalog;
                builder.UserID = userName;
                builder.Password = userPassword;
                ///////////////////////////////////////////
                return builder.ConnectionString;
            }
            return String.Empty;
        }
        #endregion
    }

}
